<?php
// $Id$

/**
 * Controller_Vote 控制器
 */
class Controller_Train extends Controller_Abstract
{
    function actionSearch()
    {
        if(!isset($this->_context->data)|| !isset($this->_context->search_type))
        {
            return "failed:0";//参数错误
        }
        require 'genrss.php';
        $rss_gen=new RSS('电信', "wadiff.cn", '火车时刻表', '');

        $date = Date('Y-m-d H:i:s', time()); 


        $search_type = $this->_context->search_type;//搜索类型
        if($search_type == 1010)
        {
            //按照车次搜索
            $train_num =  $this->_context->data;//搜索类型

            $trains = Train::find('train_num like ?', $train_num)->getAll();
            foreach($trains as $col)
            {
                $Train_Num = $col->train_num;
                $Type = $col->type;
                $S_No = $col->s_no;
                $Station = $col->station;
                $Day = $col->day;
                $A_Time = $col->a_time;
                $D_Time = $col->d_time;
                $Distance = $col->distance;
                $P1 = $col->p1;
                $P2 = $col->p2;
                $P3 = $col->p3;
                $P4 = $col->p4;
                $rss_gen->AddItem_Train($Train_Num, $Type, $S_No, $Station, $Day, $A_Time, $D_Time, $Distance, $P1, $P2, $P3, $P4);
            }
            echo $rss_gen->DisplayTrain();
            return;
        }
        else if($search_type == 1011)
        {
            //按照始发站搜索
            $Train_StartStation=$this->_context->data;

            $trains = Train::find('station = ?', $Train_StartStation)->getAll();
            foreach($trains as $col)
            {
                $Train_Num = $col->train_num;
                $Type = $col->type;
                $S_No = $col->s_no;
                $Station = $col->station;
                $Day = $col->day;
                $A_Time = $col->a_time;
                $D_Time = $col->d_time;
                $Distance = $col->distance;
                $P1 = $col->p1;
                $P2 = $col->p2;
                $P3 = $col->p3;
                $P4 = $col->p4;
                $rss_gen->AddItem_Train($Train_Num, $Type, $S_No, $Station, $Day, $A_Time, $D_Time, $Distance, $P1, $P2, $P3, $P4);
            }
            return $rss_gen->DisplayTrain();
        }

        else if($search_type == 1012)
        {
            //按照始发站和终点站搜索
            $Train_StartStation=$this->_context->data;
            $Train_DestStation=$this->_context->data_2;
            
            if( !isset($this->_context->data_2))
            {
                return "failed:0";//参数错误
            }
            if($Train_StartStation == $Train_DestStation)
            {
                return "failed:1";//始发站和目标站相同
            }
/*
SELECT * 
FROM `tianyi_train` A, `tianyi_train` B
WHERE A.station = '"西安"'
AND B.station = '"武昌"'
AND A.S_No < B.S_No
AND A.Train_Num = B.Train_Num
LIMIT 0 , 30
select * from tianyi_train t1  
where t1.station='"无锡"'
and exists (select 1 from tianyi_train t2 where t2.Train_Num=t1.Train_Num and t2.station='"常州"')
select distinct t1.train_num from tianyi_train t1  
where t1.station='"无锡"'
and t1.train_num in(select t2.train_num from tianyi_train t2 where t2.station='"常州"')
*/
            //先找出车次从 A地到B 地
            $sql = "select distinct t1.train_num from tianyi_train t1 where t1.station='".$Train_StartStation."' and t1.train_num in(select t2.train_num from tianyi_train t2 where t2.station='".$Train_DestStation."')";
            $dbconn = QDB::getConn();
            $handle = $dbconn->execute($sql);
            $train_nums = $handle->fetchAll();
            $handle->free();

            foreach($train_nums as $row)
            {
                $train_num = $row[0];
                $train_1 = Train::find('train_num = ? and station = ?', $train_num, $Train_StartStation)->query();
                $train_2 = Train::find('train_num = ? and station = ?', $train_num, $Train_DestStation)->query();
                if(isset($train_1) && isset($train_1->train_num))
                {
                    $Train_Num = $train_1->train_num;
                    $Type = $train_1->type;
                    $S_No = $train_1->s_no;
                    $Station = $train_1->station;
                    $Day = $train_1->day;
                    $A_Time = $train_1->a_time;
                    $D_Time = $train_1->d_time;
                    $Distance = $train_1->distance;
                    $P1 = $train_1->p1;
                    $P2 = $train_1->p2;
                    $P3 = $train_1->p3;
                    $P4 = $train_1->p4;
                    $rss_gen->AddItem_Train($Train_Num, $Type, $S_No, $Station, $Day, $A_Time, $D_Time, $Distance, $P1, $P2, $P3, $P4);
                }
                if(isset($train_2) && isset($train_2->train_num))
                {
                    $Train_Num = $train_2->train_num;
                    $Type = $train_2->type;
                    $S_No = $train_2->s_no;
                    $Station = $train_2->station;
                    $Day = $train_2->day;
                    $A_Time = $train_1->a_time;
                    $D_Time = $train_2->d_time;
                    $Distance = $train_2->distance;
                    $P1 = $train_2->p1;
                    $P2 = $train_2->p2;
                    $P3 = $train_2->p3;
                    $P4 = $train_2->p4;
                    $rss_gen->AddItem_Train($Train_Num, $Type, $S_No, $Station, $Day, $A_Time, $D_Time, $Distance, $P1, $P2, $P3, $P4);
                }
            }
            return $rss_gen->DisplayTrain();
            //从车次里面找两个表项，一个是始发站，一个是终点站
        }
        else
        {
            return "failed:0";//参数错误
        }
    }

}

?>

